AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

util.PrecacheSound( "explode_9" )
util.PrecacheSound( "ambient/levels/labs/electric_explosion4.wav" )
util.PrecacheSound( "ambient/levels/labs/electric_explosion3.wav" )
util.PrecacheSound( "ambient/levels/labs/electric_explosion1.wav" )
util.PrecacheSound( "ambient/explosions/exp2.wav" )
util.PrecacheSound( "k_lab.ambient_powergenerators" )
util.PrecacheSound( "ambient/machines/thumper_startup1.wav" )
util.PrecacheSound( "coast.siren_citizen" )
util.PrecacheSound( "common/warning.wav" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32
local Energy_Increment = 200

local HW_Increment = 1
local minNEU = 1000
function ENT:Initialize()
	self.BaseClass.Initialize(self)
	self.Active = 0
	self.damaged = 0
	self.critical = 0
	self.hwcount = 0
	self.time = 0
	if not (WireAddon == nil) then
		self.WireDebugName = self.PrintName
		self.Inputs = Wire_CreateInputs(self.Entity, { "On" })
		self.Outputs = Wire_CreateOutputs(self.Entity, { "On", "Output" })
	end
	self.Entity:SetColor( 10, 96, 255, 255 )
end

function ENT:TurnOn()
	if (self.Active == 0 and RD_GetResourceAmount(self, "neutrions") > minNEU) then
		self.Active = 1
		self.Entity:EmitSound( "k_lab.ambient_powergenerators" )
		self.Entity:EmitSound( "ambient/machines/thumper_startup1.wav" )
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "On", 1) end
		self:SetOOO(1)
	end
end

function ENT:TurnOff()
	if (self.Active == 1) then
		self.Active = 0
		self.Entity:StopSound( "k_lab.ambient_powergenerators" )
		self.Entity:StopSound( "coast.siren_citizen" )
		if not (WireAddon == nil) then 
			Wire_TriggerOutput(self.Entity, "On", 0)
			Wire_TriggerOutput(self.Entity, "Output", 0)
		end
		self:SetOOO(0)
	end
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		self:SetActive(value)
	end
end

function ENT:Damage()
	if (self.damaged == 0) then self.damaged = 1 end
end

function ENT:Repair()
	self.Entity:SetColor( 10, 96, 255, 255 )
	self.health = self.maxhealth
	self.damaged = 0
	self.critical = 0
	self.hwcount = 0
	self.Entity:StopSound( "coast.siren_citizen" )
end


function ENT:OnRemove()
	self.BaseClass.OnRemove(self)
	self.Entity:StopSound( "k_lab.ambient_powergenerators" )
	self.Entity:StopSound( "coast.siren_citizen" )	
end

function ENT:Extract_Energy()
	local inc = Energy_Increment

	--the money shot!
	if (inc > 0) then
		if(RD_GetResourceAmount(self, "neutrions") >= minNEU) then
			local inc = math.random(2,7)*Energy_Increment
			RD_ConsumeResource(self, "neutrions", inc)
			RD_SupplyResource(self, "energy", (inc*0.7)) 
			RD_SupplyResource(self, "heavy water", (inc*0.3)) 
			
			else
			self:TurnOff()
		end
	end
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Output", inc) end

end


function ENT:Think()
	self.BaseClass.Think(self)
	
	if (self.Active == 1) then
		self:Extract_Energy()
	end
	
	if (self.damaged == 1) then
		
	end
	
	self.Entity:NextThink(CurTime() + 1)
	return true
end

